package org.hxzon.cyclone.pfgen;

import org.hxzon.cyclone.Particle;
import org.hxzon.cyclone.Vector3;

//Buoyancy，浮力发生器
public class ParticleBuoyancy implements ParticleForceGenerator {
    //物体总高度
    private double maxDepth;

    //物体的体积
    private double volume;

    //水深
    private double waterHeight;

    //液体密度
    private double liquidDensity;

    public ParticleBuoyancy(double maxDepth, double volume, double waterHeight, double liquidDensity) {
        this.maxDepth = maxDepth;
        this.volume = volume;
        this.waterHeight = waterHeight;
        this.liquidDensity = liquidDensity;
    }

    //默认水的密度
    public ParticleBuoyancy(double maxDepth, double volume, double waterHeight) {
        this(maxDepth, volume, waterHeight, 1000.0f);
    }

    public void updateForce(Particle particle, double duration) {
        {
            // 物体的上顶点位置
            double depth = particle.getPosition().y;

            // 物体已离开水面
            if (depth >= waterHeight + maxDepth) {
                return;
            }
            Vector3 force = new Vector3(0, 0, 0);

            // 物体完全浸在水中
            if (depth <= waterHeight - maxDepth) {
                force.y = liquidDensity * volume;
                particle.addForce(force);
                return;
            }

            // 物体浸入水中的深度/物体总高度，得到物体的浸入体积比例，为何除以2？
            force.y = liquidDensity * volume * (depth - maxDepth - waterHeight) / 2 * maxDepth;
            particle.addForce(force);
        }
    }

}
